# utils/AutoEmail.py
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

# QQ邮箱信息
smtp_server = 'smtp.qq.com'
smtp_port = 465  # SSL端口
sender_email = '984611330@qq.com'
auth_code = 'clxnjkfalwrcbeff'  # 注意是授权码，不是QQ密码

class AutoEmail:
    def __init__(self, smtp_server, smtp_port, sender_email, auth_code):
        """
        :param smtp_server: SMTP服务器地址，如 smtp.qq.com
        :param smtp_port: SMTP端口，如 465
        :param sender_email: 发件人邮箱地址
        :param auth_code: 邮箱授权码（QQ邮箱需开启SMTP并生成）
        """
        self.smtp_server = smtp_server
        self.smtp_port = smtp_port
        self.sender_email = sender_email
        self.auth_code = auth_code

    def send_suggestion_notice(self, to_email: str, suggestion_data: dict) -> bool:
        """
        发送建议受理通知邮件
        :param to_email: 接收人邮箱（用户填写）
        :param suggestion_data: 包含建议信息的字典
        :return: True / False
        """
        subject = '您提交的网址推荐已受理'
        html_content = f"""
        <html>
        <body>
            <p>尊敬的用户，您好！</p>
            <p>您于 <strong>{suggestion_data.get('date', '')}</strong> 推荐的网址已受理，我们将于两个工作日内进行处理。</p>
            <hr>
            <p><strong>推荐信息：</strong></p>
            <ul>
                <li><strong>网站名称：</strong>{suggestion_data.get('site_name')}</li>
                <li><strong>网站链接：</strong><a href="{suggestion_data.get('site_url')}">{suggestion_data.get('site_url')}</a></li>
                <li><strong>网站描述：</strong>{suggestion_data.get('site_description')}</li>
                <li><strong>网站LOGO：</strong>{suggestion_data.get('site_logo')}</li>
                <li><strong>所属分类ID：</strong>{suggestion_data.get('category_id')}</li>
                <li><strong>提交IP：</strong>{suggestion_data.get('ip')}</li>
            </ul>
            <p style="color:gray;font-size:12px;">感谢您的参与与支持！</p>
        </body>
        </html>
        """

        try:
            msg = MIMEMultipart()
            msg['From'] = Header(self.sender_email)
            msg['To'] = Header(to_email)
            msg['Subject'] = Header(subject, 'utf-8')
            msg.attach(MIMEText(html_content, 'html', 'utf-8'))

            server = smtplib.SMTP_SSL(self.smtp_server, self.smtp_port)
            server.login(self.sender_email, self.auth_code)
            server.sendmail(self.sender_email, [to_email], msg.as_string())
            server.quit()
            return True
        except Exception as e:
            print(f"发送邮件失败: {e}")
            return False


if __name__ == '__main__':
    # 创建AutoEmail实例
    auto_email = AutoEmail(smtp_server, smtp_port, sender_email, auth_code)

    # 示例数据
    suggestion_data = {
        'date': '2023-07-01',
        'site_name': '示例网站',
        'site_url': 'https://example.com',
        'site_description': '这是一个示例网站',
        'site_logo': 'https://example.com/logo.png',
    }
    to_email = '1399213555@qq.com'
    result = auto_email.send_suggestion_notice(to_email, suggestion_data)
    print(f"邮件发送结果：{result}")